/*
*  Copyright 2019-2025 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.jiaoyou.users.service.impl;

import me.zhengjie.modules.jiaoyou.users.domain.Users;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.zhengjie.modules.jiaoyou.users.service.UsersService;
import me.zhengjie.modules.jiaoyou.users.domain.dto.UsersQueryCriteria;
import me.zhengjie.modules.jiaoyou.users.mapper.UsersMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import me.zhengjie.utils.PageUtil;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import me.zhengjie.utils.PageResult;

/**
* @description 服务实现
* @author ztd
* @date 2025-10-28
**/
@Service
@RequiredArgsConstructor
public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users> implements UsersService {

    private final UsersMapper usersMapper;

    @Override
    public PageResult<Users> queryAll(UsersQueryCriteria criteria, Page<Object> page){
        return PageUtil.toPage(usersMapper.findAll(criteria, page));
    }

    @Override
    public List<Users> queryAll(UsersQueryCriteria criteria){
        return usersMapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(Users resources) {
        usersMapper.insert(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Users resources) {
        Users users = getById(resources.getId());
        users.copy(resources);
        usersMapper.updateById(users);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Integer> ids) {
        usersMapper.deleteBatchIds(ids);
    }

    @Override
    public void download(List<Users> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (Users users : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("手机号", users.getPhone());
            map.put("微信openid", users.getOpenid());
            map.put("昵称", users.getNickname());
            map.put("头像", users.getAvatar());
            map.put("性别", users.getGender());
            map.put("年龄", users.getAge());
            map.put("所在地", users.getLocation());
            map.put("维度", users.getLatitude());
            map.put("经度", users.getLongitude());
            map.put("签名", users.getSignature());
            map.put("创建时间", users.getCreatedAt());
            map.put("更新时间", users.getUpdatedAt());
            map.put("密码", users.getPassword());
            map.put("身高", users.getHeight());
            map.put("星座", users.getConstellation());
            map.put("职位", users.getOccupation());
            map.put("收入", users.getIncome());
            map.put("学历", users.getEducation());
            map.put("学校", users.getSchool());
            map.put("是否学生", users.getIsStudent());
            map.put("公司", users.getCompany());
            map.put("住房状态", users.getHouseStatus());
            map.put("汽车状态", users.getCarStatus());
            map.put("家乡", users.getHometown());
            map.put("工作地", users.getWorkLocation());
            map.put("婚姻状态", users.getMaritalStatus());
            map.put("是否实名认证", users.getIsRealNameVerified());
            map.put("是否头像认证", users.getIsAvatarVerified());
            map.put("背景", users.getBackground());
            map.put("标签", users.getTags());
            map.put("上级id", users.getParentId());
            map.put("状态", users.getStatus());
            map.put("店铺状态", users.getIsShop());
            map.put("是否在线", users.getIsOnline());
            map.put("最后活跃时间", users.getLastActive());
            map.put("是否认证", users.getIsVerified());
            map.put("所在城市", users.getCity());
            map.put("所在省份", users.getProvince());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}